<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
</head>
<body>
<article class="baidu_pl">
        <div id="article_content" class="article_content clearfix">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/kdoc_html_views-1a98987dfd.css">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-704d5b9767.css">

                <div id="content_views" class="markdown_views prism-tomorrow-night">
                    <svg xmlns="http://www.w3.org/2000/svg" style="display: none;">
                        <path stroke-linecap="round" d="M5,0 0,2.5 5,5z" id="raphael-marker-block" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);"></path>
                    </svg>
                    <p></p>
<div class="toc">
 <h4><a name="t0"></a>目录：</h4>
 <ul><li><a href="#Python_2" rel="nofollow" target="_self">Python基础语法（二）</a></li><li><ul><li><a href="#11_Python3__4" rel="nofollow" target="_self">11. Python3 标准库概览</a></li><li><ul><li><a href="#111___9" rel="nofollow" target="_self">11.1 文件通配符</a></li><li><a href="#112__18" rel="nofollow" target="_self">11.2 命令行参数</a></li><li><ul><li><a href="#_31" rel="nofollow" target="_self">①命令行参数：</a></li><li><a href="#_47" rel="nofollow" target="_self">②标准输入输出函数</a></li><li><a href="#_57" rel="nofollow" target="_self">延伸—标准输入、输出和错误流文件对象</a></li></ul>
    </li><li><a href="#113__194" rel="nofollow" target="_self">11.3 访问互联网</a></li><li><a href="#114___213" rel="nofollow" target="_self">11.4 日期和时间</a></li></ul>
  </li></ul>
 </li></ul>
</div>
<p></p>
<h2><a name="t1"></a><a id="Python_2"></a><a href="https://so.csdn.net/so/search?q=Python%E5%9F%BA%E7%A1%80%E8%AF%AD%E6%B3%95&amp;spm=1001.2101.3001.7020" target="_blank" class="hl hl-1" data-report-click="{&quot;spm&quot;:&quot;1001.2101.3001.7020&quot;,&quot;dest&quot;:&quot;https://so.csdn.net/so/search?q=Python%E5%9F%BA%E7%A1%80%E8%AF%AD%E6%B3%95&amp;spm=1001.2101.3001.7020&quot;,&quot;extra&quot;:&quot;{\&quot;searchword\&quot;:\&quot;Python基础语法\&quot;}&quot;}" data-tit="Python基础语法" data-pretit="python基础语法">Python基础语法</a>（二）</h2>
<h3><a name="t2"></a><a id="11_Python3__4"></a>11. Python3 标准库概览</h3>
<blockquote>
 <p>  简单提一嘴，虽然本文title为<span class="words-blog hl-git-1" data-tit="Python3标准库" data-pretit="python3标准库">Python3标准库</span>概览，但是考虑到有不少人是刚刚接触Python，所以此处就简单提到几个比较常用的库，同时在某些库我会附带很详细的讲解文章链接，如果有兴趣就钻研下去；而且随着本专栏后续文章的更新，会陆陆续续带领大家接触越来越多的Python3标准库，我也不保证本文后面是否会做相应的增加哦~</p>
</blockquote>
<h4><a name="t3"></a><a id="111___9"></a>11.1 文件通配符</h4>
<p>  <a href="https://so.csdn.net/so/search?q=glob&amp;spm=1001.2101.3001.7020" target="_blank" class="hl hl-1" data-report-click="{&quot;spm&quot;:&quot;1001.2101.3001.7020&quot;,&quot;dest&quot;:&quot;https://so.csdn.net/so/search?q=glob&amp;spm=1001.2101.3001.7020&quot;,&quot;extra&quot;:&quot;{\&quot;searchword\&quot;:\&quot;glob\&quot;}&quot;}" data-tit="glob" data-pretit="glob">glob</a>模块提供了一个函数用于从目录通过通配符搜索，返回对应文件列表:</p>
<pre data-index="0" class="set-code-show prettyprint"><code class="prism language-python has-numbering" onclick="mdcp.copyCode(event)" style="position: unset;"><span class="token operator">&gt;&gt;</span><span class="token operator">&gt;</span> <span class="token keyword">import</span> glob
<span class="token operator">&gt;&gt;</span><span class="token operator">&gt;</span> glob<span class="token punctuation">.</span>glob<span class="token punctuation">(</span><span class="token string">'*.py'</span><span class="token punctuation">)</span>
<span class="token punctuation">[</span><span class="token string">'primes.py'</span><span class="token punctuation">,</span> <span class="token string">'random.py'</span><span class="token punctuation">,</span> <span class="token string">'quote.py'</span><span class="token punctuation">]</span>
<div class="hljs-button {2}" data-title="复制"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li></ul></pre>
<h4><a name="t4"></a><a id="112__18"></a>11.2 命令行参数</h4>
<p>乍一看这个名字，初学者可能会有点懵，所以先来讲讲为啥我会在这么早就讲这个玩意。<br> 首先，我们要认识到一个很关键的点是—我们学一门编程语言肯定要学的一个知识点是输入和输出。<br>   【程序通过输入接受待处理的数据；然后执行相应的处理；最后通过输出返回处理结果】</p>
<p>而讲到这，就又得提一下Python常见的几种交互方式：</p>
<ol><li>命令行参数</li><li>标准输入和输出函数</li><li>文件输入和输出（这个涉及到文件操作，而文件操作初学还碰不到，但是后面会讲到~）</li><li>图形化用户界面（后面也会提一嘴~）</li></ol>
<p>看到没，这不就绕回来了！下面咱们就一起来看看命令行参数：</p>
<h5><a id="_31"></a>①命令行参数：</h5>
<p>  用户可以在命令行中运行Python程序时输入参数，并在程序中通过<code>sys.argv</code>访问 。</p>
<ul><li>①通用工具脚本经常调用命令行参数。这些命令行参数以链表形式存储于 sys 模块的 argv 变量。例如在命令行中执行 “<code>python demo.py one two three</code>” 后输出为：[demo.py’, ‘one’, ‘two’, ‘three’]</li></ul>
<p>demo.py文件：</p>
<pre data-index="1" class="set-code-show prettyprint"><code class="prism language-python has-numbering" onclick="mdcp.copyCode(event)" style="position: unset;"><span class="token keyword">import</span> sys

<span class="token keyword">print</span><span class="token punctuation">(</span>sys<span class="token punctuation">.</span>argv<span class="token punctuation">)</span>    <span class="token comment"># sys.argv 是命令行参数列表。</span>
<div class="hljs-button {2} mddef" data-title="复制" data-report-click="{&quot;spm&quot;:&quot;3001.10243&quot;}"></div></code><ul class="pre-numbering" style="opacity: 0.344613;"><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li></ul>'<div class="code-edithtml active " data-title="运行"><div data-report-view="{&quot;spm&quot;:&quot;3001.10182&quot;,&quot;extra&quot;:{&quot;index&quot;:0,&quot;ab&quot;:&quot;exp1&quot;}}" data-report-click="{&quot;spm&quot;:&quot;3001.10182&quot;,&quot;extra&quot;:{&quot;index&quot;:0,&quot;ab&quot;:&quot;exp1&quot;}}" class="code-edithtml-box code-edithtml-box0"><i></i>运行<div><button>运行</button></div></div></div></pre>
<p><strong>注：</strong> sys.argv[0] 表示脚本名（也就是文件名）。 其余的依此类推。</p>
<h5><a id="_47"></a>②标准输入输出函数</h5>
<p>Python内置的标准输入输出函数分别是input()和print()函数。</p>
<ul><li> <p>input函数格式为：<code>input([prompt])</code><br> 功能：input函数提示用户输入，并返回用户从控制台输入的内容（注意是字符串格式）</p> </li><li> <p>print函数格式：<code>print(value,...,sep=' ',end='\n',file='sys.stdout',flush=False)</code><br> 功能：print函数用于打印一行内容，即将多个以分隔符（sep，默认为空格）分隔的值（value1,value2,…以逗号分隔的值）写入到指定文件流（file,默认控制台sys.stdout）。参数end默认指定为换行符，flush指定是否强制写入到流；</p> </li></ul>
<h5><a id="_57"></a>延伸—标准输入、输出和错误流文件对象</h5>
<p>  sys 还有 stdin，stdout 和 stderr 属性，这就是三个文件流对象（这是程序启动时，python自动创建的哦~ ），即标准输入流文件对象、标准输出流文件对象和错误输出流文件对象 ：sys.stdin，sys.stdout，sys.stderr。 <strong>标准输入流文件对象默认对应控制台键盘。标准输出流文件对象和错误输出流文件对象默认对应于控制台，其区别仅在于<font color="red">后者是非缓冲的</font>~</strong></p>
<p>  偷偷提一嘴：print实际上就是调用<code>sys.stdout.write</code>方法；那么input实际上调用的也就不言而喻了。而且我前面也提过这一茬哦~ 不信你看：</p>
<pre data-index="2" class="set-code-show prettyprint"><code class="prism language-python has-numbering" onclick="mdcp.copyCode(event)" style="position: unset;"><span class="token comment"># -*- coding: utf-8 -*-</span>
<span class="token comment"># __author__ = 孤寒者</span>

<span class="token keyword">import</span> sys

<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"hello world"</span><span class="token punctuation">)</span>

<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"hello world"</span><span class="token punctuation">,</span> <span class="token builtin">file</span><span class="token operator">=</span>sys<span class="token punctuation">.</span>stdout<span class="token punctuation">)</span>

sys<span class="token punctuation">.</span>stdout<span class="token punctuation">.</span>write<span class="token punctuation">(</span><span class="token string">"hello world"</span><span class="token punctuation">)</span>
<div class="hljs-button {2} mddef" data-title="复制" data-report-click="{&quot;spm&quot;:&quot;3001.10243&quot;}"></div></code><ul class="pre-numbering" style="opacity: 0.344613;"><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li></ul>'<div class="code-edithtml active " data-title="运行"><div data-report-view="{&quot;spm&quot;:&quot;3001.10182&quot;,&quot;extra&quot;:{&quot;index&quot;:1,&quot;ab&quot;:&quot;exp1&quot;}}" data-report-click="{&quot;spm&quot;:&quot;3001.10182&quot;,&quot;extra&quot;:{&quot;index&quot;:1,&quot;ab&quot;:&quot;exp1&quot;}}" class="code-edithtml-box code-edithtml-box1"><i></i>运行<div><button>运行</button></div></div></div></pre>
<p><img src="https://i-blog.csdnimg.cn/blog_migrate/4fb1d062b7c713be29db3cc1f8bb5a83.png" alt="在这里插入图片描述"></p>
<p><strong>实例一： stdout 重定向</strong></p>
<pre data-index="3" class="set-code-show prettyprint"><code class="prism language-python has-numbering" onclick="mdcp.copyCode(event)" style="position: unset;"><span class="token comment"># __author__ = 孤寒者</span>
<span class="token keyword">import</span> sys

f <span class="token operator">=</span> <span class="token builtin">open</span><span class="token punctuation">(</span><span class="token string">'out.log'</span><span class="token punctuation">,</span> <span class="token string">'w'</span><span class="token punctuation">)</span>
sys<span class="token punctuation">.</span>stdout <span class="token operator">=</span> f				<span class="token comment"># 此处将标准输出流文件对象重定向为out.log文件</span>

<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"hello world!"</span><span class="token punctuation">)</span>		<span class="token comment"># 调用print()函数，其打印的内容就不会在终端显示，而是输出到文件out.log文件中！</span>
<div class="hljs-button {2} mddef" data-title="复制" data-report-click="{&quot;spm&quot;:&quot;3001.10243&quot;}"></div></code><ul class="pre-numbering" style="opacity: 0.344613;"><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li></ul>'<div class="code-edithtml active " data-title="运行"><div data-report-view="{&quot;spm&quot;:&quot;3001.10182&quot;,&quot;extra&quot;:{&quot;index&quot;:2,&quot;ab&quot;:&quot;exp1&quot;}}" data-report-click="{&quot;spm&quot;:&quot;3001.10182&quot;,&quot;extra&quot;:{&quot;index&quot;:2,&quot;ab&quot;:&quot;exp1&quot;}}" class="code-edithtml-box code-edithtml-box2"><i></i>运行<div><button>运行</button></div></div></div></pre>
<p><img src="https://i-blog.csdnimg.cn/blog_migrate/1ecbed0ba319e3da702e517df4545963.png" alt="请添加图片描述"></p>
<p><em>需要注意的是：</em></p>
<p>  <strong>即使在 stdout 被重定向时，sys.stderr也可以用于在终端显示警告和错误信息。</strong></p>
<pre data-index="4" class="set-code-show prettyprint"><code class="prism language-python has-numbering" onclick="mdcp.copyCode(event)" style="position: unset;"><span class="token keyword">import</span> sys

f <span class="token operator">=</span> <span class="token builtin">open</span><span class="token punctuation">(</span><span class="token string">'out.log'</span><span class="token punctuation">,</span> <span class="token string">'w'</span><span class="token punctuation">)</span>
sys<span class="token punctuation">.</span>stdout <span class="token operator">=</span> f				<span class="token comment"># 此处将标准输出流文件对象重定向为out.log文件</span>

<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"hello world!"</span><span class="token punctuation">)</span>		<span class="token comment"># 调用print()函数，其打印的内容就不会在终端显示，而是输出到文件out.log文件中！</span>

sys<span class="token punctuation">.</span>stderr<span class="token punctuation">.</span>write<span class="token punctuation">(</span><span class="token string">'Warning, log file not found starting a new one\n'</span><span class="token punctuation">)</span>
<div class="hljs-button {2} mddef" data-title="复制" data-report-click="{&quot;spm&quot;:&quot;3001.10243&quot;}"></div></code><ul class="pre-numbering" style="opacity: 0.344613;"><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li></ul>'<div class="code-edithtml active " data-title="运行"><div data-report-view="{&quot;spm&quot;:&quot;3001.10182&quot;,&quot;extra&quot;:{&quot;index&quot;:3,&quot;ab&quot;:&quot;exp1&quot;}}" data-report-click="{&quot;spm&quot;:&quot;3001.10182&quot;,&quot;extra&quot;:{&quot;index&quot;:3,&quot;ab&quot;:&quot;exp1&quot;}}" class="code-edithtml-box code-edithtml-box3"><i></i>运行<div><button>运行</button></div></div></div></pre>
<p><img src="https://i-blog.csdnimg.cn/blog_migrate/0b2a3acfc9d3400b690c334896da23b9.png" alt="请添加图片描述"></p>
<p><strong>实例二：stdin重定向</strong><br> 首先，通过一个实例带大家理解一句话：<font color="red">程序可以从输入流（sys.stdin）中读取数据直至输入流为空！</font></p>
<ul><li> <p>程序可以从输入流（sys.stdin）中读取数据直至输入流为空。理论上输入流大小是无限制的。一般是使用组合键Ctrl+D或者Ctrl+Z指示输入流结束。</p> </li><li> <p>大多数场景下，都使用的是标准输入，因为标准输入的话，我们人可以与代码进行交互，而且可以输入大量的数据（命令行参数是有长度限制的）；而且使用标准输入还可以通过重定向标准输入的源为文件或是其他程序的输出等，从而实现输入的灵活性~</p> </li></ul>
<pre data-index="5" class="set-code-show prettyprint"><code class="prism language-python has-numbering" onclick="mdcp.copyCode(event)" style="position: unset;"><span class="token comment"># -*- coding: utf-8 -*-</span>
<span class="token comment"># __author__ = 孤寒者</span>

<span class="token keyword">import</span> sys

num <span class="token operator">=</span> <span class="token number">0.0</span>
<span class="token keyword">for</span> line <span class="token keyword">in</span> sys<span class="token punctuation">.</span>stdin<span class="token punctuation">:</span>
    num <span class="token operator">+=</span> <span class="token builtin">float</span><span class="token punctuation">(</span>line<span class="token punctuation">)</span>
<span class="token builtin">sum</span> <span class="token operator">=</span> num
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'求和：'</span><span class="token punctuation">,</span>num<span class="token punctuation">)</span>
<div class="hljs-button {2} mddef" data-title="复制" data-report-click="{&quot;spm&quot;:&quot;3001.10243&quot;}"></div></code><ul class="pre-numbering" style="opacity: 0.344613;"><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li></ul>'<div class="code-edithtml active " data-title="运行"><div data-report-view="{&quot;spm&quot;:&quot;3001.10182&quot;,&quot;extra&quot;:{&quot;index&quot;:4,&quot;ab&quot;:&quot;exp1&quot;}}" data-report-click="{&quot;spm&quot;:&quot;3001.10182&quot;,&quot;extra&quot;:{&quot;index&quot;:4,&quot;ab&quot;:&quot;exp1&quot;}}" class="code-edithtml-box code-edithtml-box4"><i></i>运行<div><button>运行</button></div></div></div></pre>
<p><img src="https://i-blog.csdnimg.cn/blog_migrate/74869c081810ac6380e975302d03dbb7.png" alt="在这里插入图片描述"></p>
<p>然后，我们实现输入流（sys.stdin）重定向到文件，看一看会是怎样的效果：</p>
<p><img src="https://i-blog.csdnimg.cn/blog_migrate/ff08a5063558cb4e7738afad60fb3f64.png" alt="在这里插入图片描述"></p>
<pre data-index="6" class="set-code-show prettyprint"><code class="prism language-python has-numbering" onclick="mdcp.copyCode(event)" style="position: unset;"><span class="token comment"># -*- coding: utf-8 -*-</span>
<span class="token comment"># __author__ = 孤寒者</span>

<span class="token keyword">import</span> sys

f <span class="token operator">=</span> <span class="token builtin">open</span><span class="token punctuation">(</span><span class="token string">'out.log'</span><span class="token punctuation">,</span> <span class="token string">'r'</span><span class="token punctuation">)</span>
sys<span class="token punctuation">.</span>stdin <span class="token operator">=</span> f		<span class="token comment"># 此处将标准输入流文件对象重定向为out.log文件</span>

f<span class="token operator">=</span> <span class="token builtin">input</span><span class="token punctuation">(</span><span class="token string">'请输入：'</span><span class="token punctuation">)</span> <span class="token comment"># 调用input()函数，程序可以从输入流sys.stdin指定的对象读取数据直到输入流为空！但是此处重定向的是文件，我们会发现input是只接收第一行内容（止于换行符“\n”），关于为啥会这样，希望读者认真思考，在评论区留下你的见解，要认真思考哦~</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>f<span class="token punctuation">)</span>
<div class="hljs-button {2} mddef" data-title="复制" data-report-click="{&quot;spm&quot;:&quot;3001.10243&quot;}"></div></code><ul class="pre-numbering" style="opacity: 0.344613;"><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li></ul>'<div class="code-edithtml active " data-title="运行"><div data-report-view="{&quot;spm&quot;:&quot;3001.10182&quot;,&quot;extra&quot;:{&quot;index&quot;:5,&quot;ab&quot;:&quot;exp1&quot;}}" data-report-click="{&quot;spm&quot;:&quot;3001.10182&quot;,&quot;extra&quot;:{&quot;index&quot;:5,&quot;ab&quot;:&quot;exp1&quot;}}" class="code-edithtml-box code-edithtml-box5"><i></i>运行<div><button>运行</button></div></div></div></pre>
<p><img src="https://i-blog.csdnimg.cn/blog_migrate/17594849967d9e2d9eef0b745e5b6233.png" alt="在这里插入图片描述"></p>
<p><strong>实例三：stderr重定向</strong></p>
<p>  我们主动制造1/0的报错，但是由于对错误输出进行了重定向，所以终端不会有任何输出，但在重定向的error.log文件可以看到本该显示在终端的报错信息：</p>
<pre data-index="7" class="set-code-show prettyprint"><code class="prism language-python has-numbering" onclick="mdcp.copyCode(event)" style="position: unset;"><span class="token comment"># __author__ = 孤寒者</span>
<span class="token keyword">import</span> sys

f <span class="token operator">=</span> <span class="token builtin">open</span><span class="token punctuation">(</span><span class="token string">'error.log'</span><span class="token punctuation">,</span> <span class="token string">'w'</span><span class="token punctuation">)</span>
sys<span class="token punctuation">.</span>stderr <span class="token operator">=</span> f

<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token operator">/</span><span class="token number">0</span><span class="token punctuation">)</span>    <span class="token comment"># 肯定会报错</span>
<div class="hljs-button {2}" data-title="复制"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li></ul></pre>
<p><img src="https://i-blog.csdnimg.cn/blog_migrate/cd94f07a128a00936957c0695526a36c.png" alt="请添加图片描述"></p>
<p><strong>恢复标准输入、输出和错误流为默认值的代码如下：</strong></p>
<pre data-index="8" class="set-code-show prettyprint"><code class="prism language-python has-numbering" onclick="mdcp.copyCode(event)" style="position: unset;"><span class="token comment"># -*- coding: utf-8 -*-</span>
<span class="token comment"># __author__ = 孤寒者</span>
<span class="token keyword">import</span> sys

sys<span class="token punctuation">.</span>stdin <span class="token operator">=</span> sys<span class="token punctuation">.</span>__stdin__
sys<span class="token punctuation">.</span>stdout <span class="token operator">=</span> sys<span class="token punctuation">.</span>__stdout__
sys<span class="token punctuation">.</span>stderr <span class="token operator">=</span> sys<span class="token punctuation">.</span>__stderr__
<div class="hljs-button {2} mddef" data-title="复制" data-report-click="{&quot;spm&quot;:&quot;3001.10243&quot;}"></div></code><ul class="pre-numbering" style="opacity: 0.344613;"><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li></ul>'<div class="code-edithtml active " data-title="运行"><div data-report-view="{&quot;spm&quot;:&quot;3001.10182&quot;,&quot;extra&quot;:{&quot;index&quot;:6,&quot;ab&quot;:&quot;exp1&quot;}}" data-report-click="{&quot;spm&quot;:&quot;3001.10182&quot;,&quot;extra&quot;:{&quot;index&quot;:6,&quot;ab&quot;:&quot;exp1&quot;}}" class="code-edithtml-box code-edithtml-box6"><i></i>运行<div><button>运行</button></div></div></div></pre>
<p><strong>小知识拓展：</strong><br>   终止脚本的最直接方法是使用 <code>sys.exit()</code> 。</p>
<h4><a name="t5"></a><a id="113__194"></a>11.3 访问<a href="https://edu.csdn.net/cloud/pm_summit?utm_source=blogglc" target="_blank" class="hl hl-1" data-report-view="{&quot;spm&quot;:&quot;1001.2101.3001.7020&quot;,&quot;dest&quot;:&quot;https://so.csdn.net/so/search?q=%E4%BA%92%E8%81%94%E7%BD%91&amp;spm=1001.2101.3001.7020&quot;,&quot;extra&quot;:&quot;{\&quot;searchword\&quot;:\&quot;互联网\&quot;}&quot;}" data-report-click="{&quot;spm&quot;:&quot;1001.2101.3001.7020&quot;,&quot;dest&quot;:&quot;https://so.csdn.net/so/search?q=%E4%BA%92%E8%81%94%E7%BD%91&amp;spm=1001.2101.3001.7020&quot;,&quot;extra&quot;:&quot;{\&quot;searchword\&quot;:\&quot;互联网\&quot;}&quot;}" data-tit="互联网" data-pretit="互联网">互联网</a></h4>
<p>  有几个模块用于访问互联网以及处理网络通信协议。其中最简单的两个库是 urllib和<span class="words-blog hl-git-1" data-report-view="{&quot;spm&quot;:&quot;1001.2101.3001.10283&quot;,&quot;extra&quot;:&quot;{\&quot;words\&quot;:\&quot;requests\&quot;}&quot;}" data-tit="requests" data-pretit="requests">requests</span> 。</p>
<blockquote>
 <p>requests库详解文章链接：<a href="https://gu-han-zhe.blog.csdn.net/article/details/118667559" rel="nofollow">《两万字博文教你python爬虫requests库【详解篇】》</a><br> urllib库详解文章链接：<a href="https://mp.weixin.qq.com/s/C9lXqdH3ubE0Bnp7kyTrNw" rel="nofollow">《万字博文教你Python爬虫urllib库&amp;&amp;urllib3库【详解篇】》</a></p>
</blockquote>
<pre data-index="9" class="set-code-show prettyprint"><code class="prism language-python has-numbering" onclick="mdcp.copyCode(event)" style="position: unset;"><span class="token comment"># -*- coding: utf-8 -*-</span>
<span class="token comment"># __author__ = 孤寒者</span>

<span class="token keyword">from</span> urllib<span class="token punctuation">.</span>request <span class="token keyword">import</span> urlopen

res <span class="token operator">=</span> urlopen<span class="token punctuation">(</span><span class="token string">'http://www.baidu.com/'</span><span class="token punctuation">)</span>

<span class="token keyword">print</span><span class="token punctuation">(</span>res<span class="token punctuation">.</span>read<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>decode<span class="token punctuation">(</span><span class="token string">'utf-8'</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<div class="hljs-button {2}" data-title="复制"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li></ul></pre>
<p><img src="https://i-blog.csdnimg.cn/blog_migrate/4ca976e55f3b38dc419150de8b740ee4.png" alt="在这里插入图片描述"></p>
<h4><a name="t6"></a><a id="114___213"></a>11.4 日期和时间</h4>
<ul><li> <p>datetime模块为日期和时间处理同时提供了简单和复杂的方法。</p> </li><li> <p>支持日期和时间算法的同时，实现的重点放在更有效的处理和格式化输出。</p> </li><li> <p>datetime模块详解文章链接：<a href="https://gu-han-zhe.blog.csdn.net/article/details/119212178" rel="nofollow">《Python常用模块 之 datetime模块——处理时间和日期》</a></p> </li></ul>
<pre data-index="10" class="set-code-show prettyprint"><code class="prism language-python has-numbering" onclick="mdcp.copyCode(event)" style="position: unset;"><span class="token comment"># -*- coding: utf-8 -*-</span>
<span class="token comment"># __author__ = 小小明-代码实体</span>

<span class="token keyword">from</span> datetime <span class="token keyword">import</span> date

now <span class="token operator">=</span> date<span class="token punctuation">.</span>today<span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>now<span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>now<span class="token punctuation">.</span>strftime<span class="token punctuation">(</span><span class="token string">"%m-%d-%y. %d %b %Y is a %A on the %d day of %B."</span><span class="token punctuation">)</span><span class="token punctuation">)</span>

birthday <span class="token operator">=</span> date<span class="token punctuation">(</span><span class="token number">2008</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span>
age <span class="token operator">=</span> now <span class="token operator">-</span> birthday
<span class="token keyword">print</span><span class="token punctuation">(</span>age<span class="token punctuation">.</span>days<span class="token punctuation">)</span>

<div class="hljs-button {2} mddef" data-title="复制" data-report-click="{&quot;spm&quot;:&quot;3001.10243&quot;}"></div></code><ul class="pre-numbering" style="opacity: 0.344613;"><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li><li style="color: rgb(153, 153, 153);">11</li><li style="color: rgb(153, 153, 153);">12</li><li style="color: rgb(153, 153, 153);">13</li></ul>'<div class="code-edithtml active " data-title="运行"><div data-report-view="{&quot;spm&quot;:&quot;3001.10182&quot;,&quot;extra&quot;:{&quot;index&quot;:7,&quot;ab&quot;:&quot;exp1&quot;}}" data-report-click="{&quot;spm&quot;:&quot;3001.10182&quot;,&quot;extra&quot;:{&quot;index&quot;:7,&quot;ab&quot;:&quot;exp1&quot;}}" class="code-edithtml-box code-edithtml-box7"><i></i>运行<div><button>运行</button></div></div></div></pre>
<p><img src="https://i-blog.csdnimg.cn/blog_migrate/ca61e773ce37a6e8e26bf74b1b94fa95.png" alt="在这里插入图片描述"></p>
<p> </p>
<p><span id="jump99"></span></p>
<center>
  👇🏻可通过点击下面——&gt;关注本人运营
 <strong>公众号</strong>👇🏻
</center>
<p></p>
<center>
 <font size="1">【这里也有加入我们Python大家庭的方式哦---&gt;互相学习&amp;&amp;共同进步（非诚勿扰）】</font>
</center>
                </div><div data-report-view="{&quot;mod&quot;:&quot;1585297308_001&quot;,&quot;spm&quot;:&quot;1001.2101.3001.6548&quot;,&quot;dest&quot;:&quot;https://gu-han-zhe.blog.csdn.net/article/details/122691960&quot;,&quot;extend1&quot;:&quot;pc&quot;,&quot;ab&quot;:&quot;new&quot;}"><div></div></div>
                <link href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/markdown_views-f23dff6052.css" rel="stylesheet">
                <link href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/style-e504d6a974.css" rel="stylesheet">
        </div>
        <div id="blogExtensionBox" style="width:400px;margin:auto;margin-top:12px" class="blog-extension-box"><div class="blog_extension blog_extension_type1" id="blog_extension">
          <div class="blog_extension_card" data-report-click="{&quot;spm&quot;:&quot;1001.2101.3001.6470&quot;}" data-report-view="{&quot;spm&quot;:&quot;1001.2101.3001.6470&quot;}">
            <div class="blog_extension_card_left">
            <img src="https://img-blog.csdnimg.cn/cb22802e18cd4eb2b6c5f7fdb8bac82b.png" alt="">
          </div>
            <div class="blog_extension_card_cont">
              <div class="blog_extension_card_cont_l">
                <span class="text">孤寒者</span>
                <div class="blog_extension_card_cont_r">
                  <img class="weixin" src="https://g.csdnimg.cn/extension-box/1.1.6/image/weixin.png" alt="">
                  <span>微信公众号</span>
                  <img class="go" src="https://g.csdnimg.cn/extension-box/1.1.6/image/ic_move.png" alt="">
                </div>
              </div>
              <span class="style">持续输出python爬虫领域以及web方向干货文！</span>
            </div>
          </div></div></div>
    </article>
</body>
</html>